<template>
  <div>
    <nav-bar title="质量报备">
    </nav-bar>
    <div class="maxinBox">
      <van-form>
        <van-field
          required
          v-model="form.taskName"
          name="任务类型名称"
          label="任务类型名称"
          placeholder="请输入任务类型名称"
        />
        <van-cell required is-link title="测量类型" :value="massType" @click="isMassType = true" />
        <van-action-sheet v-model="isMassType" :actions="massList" @select="chooseMass" />
        <van-field
          v-model="userId"
          name="测量人"
          label="测量人"
          disabled
          placeholder="请输入测量人"
        />
        <van-cell required title="测量日期" @click="showChooseTime = true" is-link :value="form.measureDate" />
        <van-popup v-model="showChooseTime" position="bottom" style="height: 40%">
          <van-datetime-picker
            @confirm="confirm"
            @cancel="showChooseTime = false"
            v-model="currentDate"
            type="date"
            title="选择测量时间"
            :min-date="minDate"
            :max-date="maxDate"
          />
        </van-popup>
        <van-field
          required
          class="marginTop5"
          v-model="form.measurePartQty"
          name="测量部位数量"
          label="测量部位数量"
          placeholder="请输入测量部位数量"
        />
        <van-field
          required
          v-model="form.checkPartQty"
          name="检查分项数量"
          label="检查分项数量"
          placeholder="请输入检查分项数量"
        />
        <van-field
          required
          v-model="form.passPercent"
          name="合格率"
          label="合格率"
          placeholder="请输入合格率"
        />
        <van-field
          v-model="form.remark"
          name="备注"
          label="备注"
          placeholder="请输入备注"
        />
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit" @click="onSubmit">
            提交
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
  import moment from 'moment'
  import massManageAPI from '../../../../api/workAPI/massManageAPI.js'
  export default {
    data() {
      return {
        minDate: new Date(),
        maxDate: new Date(2025, 10, 1),
        currentDate: new Date(),
        showChooseTime: false,
        massList: [],
        massType: '',
        isMassType: false,
        userId: localStorage.getItem('userName'),
        form: {
          userId: 67,
          measureDate: '',
          userId: localStorage.getItem('userId'),
          measurePartQty: '',
          taskType: '',
          checkPartQty: '',
          passPercent: '',
          remark: ''
        }
      }
    },
    methods: {
      // 日期选择确定按钮
      confirm(val) {
       this.form.measureDate = moment(val).format('YYYY-MM-DD')
       this.showChooseTime = false
      },
      // 查询测量类型列表
      getMassList() {
        massManageAPI.getMassType().then(res => {
          this.massList = res.map(item => {
            return {
              name: item.text,
              value: item.value
            }
          })
        })
      },
      // 选择测量类型
      chooseMass(item) {
        this.isMassType = false;
        this.form.taskType = item.value
        this.massType = item.name
      },
      onSubmit() {
        if (this.form.taskName === '') {
          this.$toast.fail('测量名称不能为空')
          return
        }
        if (this.form.taskType === '') {
          this.$toast.fail('请选择测量类型')
          return
        }
        if (this.form.measureDate === '') {
          this.$toast.fail('请选择测量日期')
          return
        }
        if (this.form.measurePartQty === '') {
          this.$toast.fail('测量部位数量不能为空')
          return
        }
        if (this.form.checkPartQty === '') {
          this.$toast.fail('检查分项数量不能为空')
          return
        }
        if (this.form.passPercent === '') {
          this.$toast.fail('合格率不能为空')
          return
        }
        let projectId = localStorage.getItem('projectId')
        massManageAPI.addMassData(this.form, projectId).then(res => {
          this.$router.push({
            name: 'MassManage'
          })
        })
      }
    },
    mounted() {
      this.getMassList()
    }
  }
</script>

<style scoped lang="scss">
  /deep/ .van-field--disabled .van-field__label{
    color: #646566;
  }
</style>
